package com.hankol.message

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.MessageBus
import com.baselibrary.extension.checkValue
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.DoubleClickHelper
import com.baselibrary.utils.IntentKey
import com.baselibrary.utils.SpUtils
import com.baselibrary.activity.BaseActivity
import com.hankol.message.R
import com.hankol.message.databinding.ActivityMessageMainPageBinding
import com.hankol.message.fragment.HomeMessageFragment
import com.hankol.message.viewmodel.HomeMessageViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**消息 主页（首页大改版）
 * @author: merbng
 * @description:
 */
@Route(path = ActivityRouter.MESSAGE.MESSAGE_MAIN_PAGE)
class MessageMainPageActivity : BaseActivity(), View.OnClickListener {

    private lateinit var viewModel: HomeMessageViewModel
    private lateinit var binding: ActivityMessageMainPageBinding
    override fun getLayoutId(): Int {
        return R.layout.activity_message_main_page
    }

    override fun init() {
        setStatusBarHeight(R.color.white)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        binding = getDataBinding()
        viewModel = getActivityViewModelProvider(this).get(HomeMessageViewModel::class.java)
        initView()
        initListener()
        initObserver()
    }

    private fun initListener() {
        binding.imgBack.setOnClickListener(this)
        binding.tvCreateChat.setOnClickListener(this)
    }

    private fun initView() {
        supportFragmentManager.beginTransaction().add(R.id.fl_container, HomeMessageFragment.newInstance()).commit()
    }

    private fun initObserver() {

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.img_back -> {
                finish()
            }
            R.id.tv_create_chat -> {
                if (!DoubleClickHelper.isOnDoubleClick()) {
                    SensorsDataManager.instance.brookPageElementClick("好友聊天")
                    //创建聊天---跳转搜索
                    val bundle = Bundle()
                    bundle.putInt(IntentKey.SEARCH_TYPE, 0)
                    ActivityRouter.startActivity(ActivityRouter.ACTIVITY.SEARCH_ALL, bundle)
                }
            }
        }
    }

    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN) fun onEvent(messageBus: MessageBus) {

    }

    override fun onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        super.onDestroy()
    }
}